package cn.tedu.bonfire.admin.account.dao.persist.repository.impl;

import cn.tedu.bonfire.admin.account.dao.persist.mapper.UserMapper;
import cn.tedu.bonfire.admin.account.dao.persist.repository.IUserRepository;
import cn.tedu.bonfire.admin.account.pojo.entity.User;
import cn.tedu.bonfire.admin.account.pojo.vo.UserManagementVO;
import cn.tedu.bonfire.common.pojo.po.UserAdminSearchPO;
import cn.tedu.bonfire.common.pojo.vo.PageData;
import cn.tedu.bonfire.common.util.PageInfoToPageDataConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserRepositoryImpl implements IUserRepository {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int insert(User user) {
        return userMapper.insert(user);

    }

    @Override
    public int deleteByUserId(Long userId, Integer enable) {
        return userMapper.deleteByUserId(userId,enable);
    }

    @Override
    public Long countByUserName(String username) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<User>();
        queryWrapper.eq("username",username);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public Long countByPhone(String phone) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        return userMapper.selectCount(queryWrapper);
    }




    /**
     * 修改用户名
     * @param nickname
     * @return
     */
    @Override
    public int updateNicknameByUserId(Long userId,String nickname) {

        return userMapper.updateNicknameByUserId(userId,nickname);
    }

    /**
     * 修改头像
     * @param userId
     * @param avatar
     * @return
     */
    @Override
    public int updateAvatarByUserId(Long userId, String avatar) {
        return userMapper.updateAvatarByUserId(userId,avatar);
    }



    /**
     * 分页查询用户
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageData<UserManagementVO> list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserManagementVO> list = userMapper.userManagementVO();
        PageInfo<UserManagementVO> pageInfo = new PageInfo<>(list);
        PageData<UserManagementVO> convert = PageInfoToPageDataConverter.convert(pageInfo);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @Override
    public int updateById(User user) {
        return userMapper.updateById(user);
    }

    /**
     * 搜索用户
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageData<UserAdminSearchPO> listSearchByUser(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserAdminSearchPO>user= userMapper.listSearch();
        PageInfo<UserAdminSearchPO> searchPageInfo = new PageInfo<>(user);
        return PageInfoToPageDataConverter.convert(searchPageInfo);
    }


}
